package com.stox.ui.javafx.navigator.view.tab;

import java.util.Collections;
import java.util.List;
import java.util.Locale;

import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.util.Callback;
import lombok.Delegate;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.stox.client.CountryClient;
import com.stox.client.ExchangeClient;
import com.stox.client.ScripClient;
import com.stox.dto.AbstractModel;
import com.stox.dto.Country;
import com.stox.dto.intf.AbstractModelSearchable;
import com.stox.ui.javafx.core.model.TreeViewAbstractModelSearchable;
import com.stox.ui.javafx.core.util.ExceptionHandler.ExceptionHandlingRunnable;
import com.stox.ui.javafx.core.widget.AbstractModelTreeCell;
import com.stox.ui.javafx.core.widget.AbstractModelTreeView;
import com.stox.ui.javafx.navigator.model.CountryTreeItem;
import com.stox.ui.javafx.navigator.widget.EquityContextMenu;

@Component
public class EquityNavigatorTab extends AbstractNavigatorTab implements
		InitializingBean, AbstractModelSearchable{
	private static final long serialVersionUID = 1L;

	@Autowired
	private CountryClient countryClient;
	
	@Autowired
	private ExchangeClient exchangeClient;
	
	@Autowired
	private ScripClient scripClient;
	
	@Autowired
	private EquityContextMenu equityContextMenu;
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	private AbstractModelTreeView treeView;
	
	@Delegate
	private final TreeViewAbstractModelSearchable treeViewabstractModelSearchable = new TreeViewAbstractModelSearchable();
	
	@Override
	public void afterPropertiesSet() throws Exception {
		setText(applicationContext.getMessage("Equity", null, Locale.getDefault()));
		setOnSelectionChanged(createTabSelectionHandler());
		treeViewabstractModelSearchable.setTreeView(treeView);
		treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		treeView.setCellFactory(new Callback<TreeView<AbstractModel>, TreeCell<AbstractModel>>(){
			@Override
			public TreeCell<AbstractModel> call(TreeView<AbstractModel> arg0) {
				return new AbstractModelTreeCell();
			}
		});
		
		equityContextMenu.setEquityNavigatorTab(this);
		treeView.setContextMenu(equityContextMenu);
		setabstractModelSearchable(treeViewabstractModelSearchable);
	}
	
	private EventHandler<Event> createTabSelectionHandler(){
		return new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				if(null != treeView.getRoot()) return;
				TreeItem<AbstractModel> rootItem = new TreeItem<>(null);
				treeView.setRoot(rootItem);
				treeView.showRootProperty().set(false);
				rootItem.setExpanded(true);
				setContent(treeView);
				
				new Thread( new ExceptionHandlingRunnable(){
					@Override
					public void doRun() {
						List<Country> countries = countryClient.findAll();
						Collections.sort(countries);
						for(Country country : countries) {
							CountryTreeItem countryTreeItem = applicationContext.getBean(CountryTreeItem.class);
							countryTreeItem.setValue(country);
							rootItem.getChildren().add(countryTreeItem);
							Platform.runLater(new ExceptionHandlingRunnable(){
								@Override
								public void doRun() {
									treeView.layout();
								}
							});
						}
					}
				}).start();
			}
		};
	}
	
}
